﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace DARE
{
    /// <summary>
    /// Represents the base class of any kind of button.
    /// It allows to invoke the events of the button even if they have any children attached (so if you attach a static text to a button, when you will click on the button, the button's click event will be called).
    /// </summary>
    [AttrTypeDependentSerializable(typeof(CButtonData))]
    public class CButton: CWidget
    {
        #region fields

        protected CHudTexture m_clickedTexture;
        protected CHudTexture m_releasedTexture;

        #endregion

        #region properties

        /// <summary>
        /// Gets/Sets the texture displayed when the button is clicked
        /// </summary>
        public CHudTexture ClickedTexture { get { return m_clickedTexture; } set { m_clickedTexture = value; if (m_clickedTexture != null) m_clickedTexture.SpriteBatch = SpriteBatch; } }
        
        /// <summary>
        /// Gets/Sets the texture displayed when the button is released
        /// </summary>
        public CHudTexture ReleasedTexture { get { return m_releasedTexture; } set { m_releasedTexture = value; m_texture = m_releasedTexture; if (m_releasedTexture != null) m_releasedTexture.SpriteBatch = SpriteBatch; } }

        private string m_text;
        public string Text
        { get { return m_text; } 
            set { m_text = value; Actualize(); } }
        public Color TextColor { get; set; }
        private SpriteFont m_font;
        public SpriteFont Font { get { return m_font; } set { m_font = value; Actualize(); } }
        public Vector2 TextPosition { get; set; }

        #endregion

        #region ctor

        public CButton(CHUDSystem hud, CWidget parent, string name)
            : base(hud, parent, name)
        {
            Size = new Vector2(50, 20);
            IsClickable = true;
            m_releasedTexture = hud.Skinner.GetTexture("buttonreleased");
            m_clickedTexture = hud.Skinner.GetTexture("buttonclicked");
            m_texture = m_releasedTexture;
            Font = hud.Skinner.GetSpriteFont("statictextdefault");
            Text = "";
        }

        #endregion

        #region clone

        /// <summary>
        /// Used simplify the Clone function of the classes inheriting CWidget.
        /// On the Clone function, call base.Clone(widgetToBeReturned)
        /// </summary>
        /// <param name="w">the widget constructed that will be returned by the Clone method</param>
        /// <returns>The widget given in parameters</returns>
        protected override CWidget Clone(ref CWidget w)
        {
            if ((w as CButton) != null)
            {
                (w as CButton).ClickedTexture = m_clickedTexture.Clone();
                (w as CButton).ReleasedTexture = m_releasedTexture.Clone();
                (w as CButton).IsClickable = IsClickable;
            }
            return base.Clone(ref w);
        }

        #endregion

        #region actualize

        public override void Actualize()
        {
            base.Actualize();
            if (Font != null && Text != null)
            {
                Vector2 strSize = Font.MeasureString(Text);
                TextPosition = new Vector2((m_rect.Width / 2.0f) - (strSize.X / 2.0f), (m_rect.Height / 2.0f) - (strSize.Y / 2.0f));
            }
        }

        #endregion

        #region events

        /// <summary>
        /// Method called whenever the mouse is pressed and is over this widget
        /// </summary>
        public override bool onClick(CMouse mouse)
        {
            if (!base.onClick(mouse) && IsClickable)
            {
                if (m_clickedTexture != null)
                {
                    m_clickedTexture.Rectangle = m_texture.Rectangle;
                    this.m_texture = m_clickedTexture;
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// Method called whenever the mouse is released and is over this widget and was pressed over this widget
        /// </summary>
        public override bool onRelease(CMouse mouse)
        {
            if (IsClickable && m_releasedTexture != null)
                this.m_texture = m_releasedTexture;
            return base.onRelease(mouse);
        }

        #endregion

        #region update/draw

        public override bool Draw(GameTime gameTime)
        {
            bool ret = base.Draw(gameTime);

            if (Text != "" && Text != null && Font != null)
                SpriteBatch.DrawString(Font, Text, TextPosition + new Vector2(m_rect.X, m_rect.Y), TextColor);

            return ret;
        }

        #endregion

    }
}
